package tt.dz.biz.service.smssend;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.smsConfigure.SmsConfigureService;
import tt.dz.common.AppContxtUtil;
import tt.dz.common.BusinessException;
import tt.dz.common.LogUtil;
import tt.dz.vo.SmsConfigureVO;

@Service("sendSMSMessageFactory")
public class SendSMSMessageFactory
{
  private static final Logger LOGGER = Logger.getLogger(SendSMSMessageFactory.class);
  private static final String DEFAULT_DZ_SMS_SEND_BEAN_NAME = "DZSMSMessage";
  private static Properties prop = new Properties();
  @Autowired
  @Qualifier("smsConfigureService")
  private SmsConfigureService smsConfigureService;
  private boolean isSend;
  
  static
  {
    InputStream in = null;
    try
    {
      in = DZSMSMessage.class.getClassLoader().getResourceAsStream("ms-context.properties");
      prop.load(in); 
    }
    catch (Exception e)
    {
      LogUtil.error("加载短信发送资源信息异常", e);
    }
    finally
    {
      if (in != null) {
        try
        {
          in.close();
        }
        catch (IOException e)
        {
          LOGGER.error("文件流关闭异常", e);
        }
      }
    }
  }
  
  @PostConstruct
  public void init()
  {
    this.isSend = Boolean.TRUE.toString().equals(prop.getProperty("sms.send.flag").trim());
  }
  
  public SendMessageI instance(String merchantCode)
  {
    SendMessageI DZ_SEND_SMS_SERIVCE = (SendMessageI)AppContxtUtil.getBean("DZSMSMessage");
    SmsConfigureVO vo = getMerchantSmsConfig(merchantCode);
    SendMessageI sendMessage = null;
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("当前短信是否允许发送|-->" + this.isSend);
    }
    if (vo == null) {
      sendMessage = DZ_SEND_SMS_SERIVCE;
    } else {
      sendMessage = getSendMessage(vo);
    }
    if (null == sendMessage) {
      throw new BusinessException("缺少当前短信平台的发送实现功能");
    }
    if (SendAbleConfigMessageI.class.isAssignableFrom(sendMessage.getClass())) {
      ((SendAbleConfigMessageI)sendMessage).isAbleSend(this.isSend);
    }
    if (SendMessageConfigAwareI.class.isAssignableFrom(sendMessage.getClass())) {
      ((SendMessageConfigAwareI)sendMessage).setSmsConfig(vo);
    }
    return sendMessage;
  }
  
  private SmsConfigureVO getMerchantSmsConfig(String merchantCode)
  {
    SmsConfigureVO vo = null;
    List<SmsConfigureVO> smsConfigVos = this.smsConfigureService.getConfigsByMerchantCode(merchantCode);
    if ((smsConfigVos != null) && (!smsConfigVos.isEmpty())) {
      vo = (SmsConfigureVO)smsConfigVos.get(0);
    }
    return vo;
  }
  
  private SendMessageI getSendMessage(SmsConfigureVO vo)
  {
    SendMessageI sendMessageUtil = null;
    String smsChannel = "";
    if (vo != null)
    {
      smsChannel = vo.getSmsChannel();
      smsChannel = smsChannel.replace("_", "");
      sendMessageUtil = (SendMessageI)AppContxtUtil.getBean(smsChannel + "SMSMessage");
    }
    return sendMessageUtil;
  }
}
